///
/// ArmyBodger 3
/// (c)2010 richyp
///
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ArmyBodger.Core;

namespace ArmyBodger.UI {
    /// <summary>
    /// A test form that allows direct entry of Python to the scriptcore
    /// </summary>
    public partial class ScriptEngineForm : Form {
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="core">ScriptCore reference</param>
        public ScriptEngineForm(ScriptCore core) {
            InitializeComponent();
            this.core = core;
            this.core.Execute("import clr");
            this.core.Execute("clr.AddReference(\"System.Drawing\")");
            this.core.Execute("clr.AddReference(\"System.Windows.Forms\")");
            this.core.Execute("clr.AddReference(\"ArmyBodger.Core\")");
            this.core.Execute("import System");
            this.core.Execute("from System.Windows.Forms import *");
            this.core.Execute("from System.Drawing import *");
            this.core.Execute("from ArmyBodger.Core import *");
            this.core.SetOutputStream(new PythonOutputStream(this.textBoxOutput));
        }
        /// <summary>
        /// The ScriptCore
        /// </summary>
        protected ScriptCore core = null;
        /// <summary>
        /// Execute was clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonExecute_Click(object sender, EventArgs e) {
            string script = "";
            foreach (string line in this.textBoxScript.Lines) {
                if (line == "clear()") {
                    this.textBoxOutput.Clear();
                } else {
                    script += line + "\n";
                }
            }
            this.core.Execute(script);
        }
    }

    public class PythonOutputStream : System.IO.Stream {
        protected TextBox outputBox = null;
        public PythonOutputStream(TextBox outputBox)  {
            this.outputBox = outputBox;
        }


        public override bool CanRead {
            get { return false; }
        }

        public override bool CanSeek {
            get { return false; }
        }

        public override bool CanWrite {
            get { return true; }
        }

        public override void Flush() {

        }

        public override long Length {
            get { throw new NotImplementedException(); }
        }

        public override long Position {
            get {
                throw new NotImplementedException();
            }
            set {
                throw new NotImplementedException();
            }
        }

        public override int Read(byte[] buffer, int offset, int count) {
            throw new NotImplementedException();
        }

        public override long Seek(long offset, System.IO.SeekOrigin origin) {
            throw new NotImplementedException();
        }

        public override void SetLength(long value) {
            throw new NotImplementedException();
        }

        public override void Write(byte[] buffer, int offset, int count) {
            if (count == 1 && buffer[offset] == '\n') return;
            StringBuilder builder = new StringBuilder();
            while (count > 0) {
                char ch = (char)buffer[offset];
                if (ch == '\n') {
                    this.outputBox.AppendText(builder.ToString() + "\n");
                    builder.Clear();
                } else if (ch != '\r') {
                    builder.Append(ch);
                }
                offset++;
                count--;
            }
            if (builder.Length > 0) {
                this.outputBox.AppendText(builder.ToString());
            }
        }
    }
}
